let SessionUtil = {}

/**
 * 删除所有
 */
SessionUtil.clearAll = function () {
  sessionStorage.clear()
}

SessionUtil.setCookie = function (name, value, hour) {
  const exp = new Date();
  exp.setTime(exp.getTime() + hour * 60 * 60 * 1000)
  document.cookie = name + '=' + escape(value) + ';expires=' + exp.toGMTString()
}

SessionUtil.getCookie = function (name) {
  const reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');
  const arr = document.cookie.match(reg);
  if (arr) {
    return unescape(arr[2])
  } else {
    return null
  }
}

SessionUtil.removeCookie = function (name) {
  const exp = new Date();
  exp.setTime(exp.getTime() - 1)
  const cval = this.getCookie(name);
  if (cval != null) {
    document.cookie = name + '=' + cval + ';expires=' + exp.toGMTString()
  }
}

SessionUtil.getUser = function () {
  let _user = this.getItem('tokenClaim');
  if (!_user || _user === 'undefined') {
    try {
      _user = this.getCookie('tokenClaim')
    } catch (e) {
    }
  }
  if (_user && _user !== 'undefined') {
    try {
      return JSON.parse(_user)
    } catch (e) {
    }
  }
  return {}
}

SessionUtil.getToken = function () {
  let user = this.getUser()
  if (user) {
    return user.token
  }
}

/**
 获取用户归属机构
 */
SessionUtil.getUserComCode = function () {
  return this.getUser().comCode
}
/**
 获取用户代码
 */
SessionUtil.getUserCode = function () {
  return null;
}

/**
 获取用户代码
 */
SessionUtil.getUserName = function () {
  return this.getUser().username
}

SessionUtil.setUser = function (_user) {
  _user = JSON.stringify(_user)
  // this.setItem('token', _user)
  try {
    this.setCookie('tokenClaim', _user, 0.5)
  } catch (e) {
  }
}

SessionUtil.removeUser = function () {
  this.removeItem('tokenClaim')
  try {
    this.removeCookie('tokenClaim')
  } catch (e) {
  }
}

SessionUtil.setItem = function (name, value) {
  sessionStorage.setItem(name, value)
}

SessionUtil.getItem = function (name) {
  let _value = sessionStorage.getItem(name)
  if (_value && _value !== 'undefined') {
    return _value
  }else{
    return null
  }
}

SessionUtil.removeItem = function (name) {
  sessionStorage.removeItem(name)
}

SessionUtil.removeAllItem = function () {
  sessionStorage.clear()
}

SessionUtil.setMenus = function (menus) {
  sessionStorage.setItem('menus', JSON.stringify(menus))
}

SessionUtil.getMenus = function () {
  sessionStorage.getItem('menus')
}

SessionUtil.dispatchEvent = function (eventType, value) {
  let eventSource = document.createEvent('StorageEvent')
  eventSource.initStorageEvent('storage', false, false, eventType, '', value || new Date().getTime(), window.location.protocol + '//' + window.location.host, localStorage)
  window.dispatchEvent(eventSource)
}

/**
 * 添加跨页面监听事件
 * @param eventType
 * @param fn
 */
SessionUtil.addListener = function (eventType, fn) {
  if (!window._storageListeners) {
    window._storageListeners = new Map()
    window.addEventListener('storage', function (e) {
      fn = window._storageListeners.get(e.key)
      if (fn) {
        fn(e.newValue)
      }
    }, false)
  }
  window._storageListeners.set(eventType, fn)
}
/**
 * 移除跨页面监听事件
 * @param eventType
 */
SessionUtil.removeListener = function (eventType) {
  window._storageListeners.delete(eventType)
}

let localStore = {}
/**
 本地缓存  注意 数组存储后，会变成字符串：,A,B,C
 @param value
 @param primaryKey id
 **/
localStore.store = function (value, primaryKey) {
  if (value) {
    sessionStorage.setItem(primaryKey, value)
  }
  return primaryKey
}
/**
 获取本地缓存数据：就是一个字符串
 @param key
 @param isNeedRemove  是否需要删除缓存
 **/
localStore.get = function (key, isNeedRemove) {
  let value = sessionStorage.getItem(key)
  if (isNeedRemove === true) {
    this.remove(key)
  }
  return value
}
/**
 本地缓存  注意 数组存储后，会变成字符串：,A,B,C
 @param value
 @param primaryKey id
 **/
localStore.storeJSON = function (value, primaryKey) {
  value = JSON.stringify(value)
  return localStore.store(primaryKey, value)
}
/**
 获取本地缓存数据, 因为序列化成JSON格式后获取
 @param key
 @param isNeedRemove  是否需要删除缓存
 **/
localStore.getJSON = function (key, isNeedRemove) {
  let value = sessionStorage.getItem(key)
  if (isNeedRemove === true) {
    this.remove(key)
  }
  if (value) {
    value = JSON.parse(value)
  }
  return value
}
/**
 删除本地缓存
 * @param key
 **/
localStore.remove = function (key) {
  sessionStorage.removeItem(key)
}

SessionUtil.localStore = localStore

export default SessionUtil

